<template>
<div>
  <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 警告区 -->
      <el-alert title="注意：只允许为第三级分类设置相关参数" type="warning" :closable="false">
      </el-alert>
      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类：</span>
       <!-- 选择商品分类的级联选择框 -->
          <el-cascader :options="catelist" :props="cateProps" v-model="selectedCateKeys" @change="handleChange">
          </el-cascader>
        </el-col>
      </el-row>
      <!-- tabs标签 -->
      <el-tabs v-model="activeName" @tab-click="handleTabsClick">
        <!-- 添加动态参数面板 -->
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" size="mini">添加参数</el-button>
        </el-tab-pane>
        <!--添加静态属性面板  -->
        <el-tab-pane label="静态属性" name="only">
           <el-button type="primary" size="mini">添加参数</el-button>
        </el-tab-pane>
    </el-tabs>
    </el-card>
</div>
</template>
<script>
export default {
  data () {
    return {
      catelist: [],
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        expandTrigger: 'hover',
        children: [{
          value: 'cat_id',
          label: 'cat_name',
          expandTrigger: 'hover',
          children: [{
            value: 'cat_id',
            label: 'cat_name',
            expandTrigger: 'hover'
          }]
        }]
      },
      // 级联选择框双向绑定
      selectedCateKeys: [],
      // 被激活的页签的名称
      activeName: 'many'
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      // 获取所有商品的分类列表
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) return this.$message.error('获取商品信息失败')
      this.catelist = res.data
      console.log(this.catelist)
    },
    async handleChange () {
      // if (this.selectedCateKeys.length !== 3) {
      //   this.selectedCateKeys = []
      //   return
      // }
      console.log(this.selectedCateKeys)
      // 根据所选的分类的 id 和当前所处的面板 获取对应的参数
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, { params: { sel: this.activeName } })
      if (res.meta.status !== 200) return this.$message.error('获取参数失败')
      console.log(res.data)
    },
    // tags页签点击事件处理函数
    handleTabsClick () {}
  },
  computed: {
    // 当前选中的三级分类的Id
    cateId () {
      if (this.selectedCateKeys.length === 1) {
        return this.selectedCateKeys[0]
      }
      return null
    }
  }
}
</script>
<style lang="less" scoped>
  .cat_opt {
    margin: 15px 0;
  }
</style>
